home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / mmailp.idb / usr / lib / Zmail / vacation.z / vacation
Encoding:
Text File  |  1997-01-22  |  8.5 KB  |  295 lines

  1. #! /usr/local/bin/zmail -I
  2. #
  3. # Z-Mail script that acts like the BSD "vacation" program.
  4. #
  5. # Author: Bart Schaefer
  6. # Copyright (c) 1991-94 Z-Code Software, a Division of NCD.
  7. #
  8.  
  9. #
  10. # Set the version number of this script.
  11. #
  12. set v_version = "Z-Vacation 1.8"
  13.  
  14. #
  15. # Initialize message and recipient file locations.
  16. # These names are similar to the BSD "vacation",
  17. # but the files do not have the same format.
  18. #
  19. set v_message = ~/.zvacation.msg
  20. set v_sent_to = ~/.zvacation.dir
  21.  
  22. # Make sure we know who we are acting for.
  23. if ! $?user
  24.     set user = "$LOGNAME"
  25. endif
  26.  
  27. function vacation() {
  28. #%
  29. # This function is used as a filter for all incoming mail during your
  30. # vacation.  It sends the file ~/.zvacation.msg to each person who sends
  31. # mail to you.  The automated reply is sent to the same correspondent
  32. # at most once per week, and an attempt is made to avoid replying to
  33. # mailing lists and junk mail.
  34. #
  35. # You may set the subject of the reply by placing this command in your
  36. # ~/.zmailrc file:
  37. #   set v_subject = "I am on vacation"
  38. # You may use any subject you like, but avoid using double quotes (")
  39. # except as shown above.  "I am on vacation" is used if none is set.
  40. #
  41. # You may set a list of senders to whom replies should not be set by
  42. # placing this command in your ~/.zmailrc file:
  43. #   set v_suppress = "-request -daemon Postmaster"
  44. # Any address containing the one of the listed words will not receive
  45. # a vacation reply.  Replies to addresses containing any of the strings
  46. # "-request", "-daemon", and "Postmaster" are always suppressed.  This
  47. # includes "mailer-daemon", "mush-users-request", etc. and is not case
  48. # sensitive, so "z-code!postmaster" also matches.
  49. #
  50. # To enable the vacation filter, place this command in ~/.forward:
  51. #
  52. #   |"/usr/local/bin/zmail -init /usr/lib/Zmail/vacation -receive"
  53. #
  54. # If your operating system recognizes the #! syntax for shell scripts,
  55. # you may execute this file directly:
  56. #
  57. #   |"/usr/lib/Zmail/vacation -receive"
  58. #
  59. # If your MTA is MMDF, use this entry in your ~/.maildelivery file:
  60. #
  61. #   * - pipe ? /usr/local/bin/zmail -init /usr/lib/Zmail/vacation -receive
  62. #
  63. # See the maildelivery manual pages for more information.
  64. #
  65. # If you are using the Z-Code Network License Server, you may need to tell
  66. # zmail how to contact the server by adding the "-zcnlserv host:port" option
  67. # to the zmail command in your .forward or .maildelivery file.
  68. #%
  69.     # Filter functions are given a list of messages in $input.
  70.     # Perform the vacation actions on each such message.
  71.     each $input v_action
  72. }
  73.  
  74. #
  75. # If this script is not being read by an interactive Z-Mail,
  76. # install the filter.  Note that this script is intended to
  77. # be sourced via the -init (-I) option of the zmail command.
  78. # (See the #! line at the top of this file.)  This ensures
  79. # that the vacation filter will be installed and run first,
  80. # before any other filters.
  81. #
  82. if redirect
  83.     filter vacation-mail vacation
  84. endif
  85.  
  86. function v_action() {
  87. #%
  88. # This function expects a single message number as an argument.
  89. # That message is checked to see whether a reply should be sent.
  90. # If the check succeeds, a reply is generated.
  91. #%
  92.     v_check $1
  93.     if $status == 0
  94.     v_mail $1
  95.     endif
  96.     return 0
  97. }
  98.  
  99. function v_check() {
  100. #%
  101. # This function is executed to check each arriving message to determine
  102. # whether an automated reply should be sent.  It searches the logfile
  103. # maintained by v_mail to see if a reply has been sent to the current
  104. # author within the past week.  It also checks for Precedence: bulk and
  105. # Precedence: junk, and for messages from -REQUEST@ (mailing lists).
  106. #
  107. # If any of the checks succeeds, v_check returns -1 to indicate that
  108. # no reply should be sent to the message in question.
  109. #%
  110.     # Check that the reply message file exists.
  111.     if ! -e $v_message
  112.     return -1
  113.     endif
  114.  
  115.     # Set the current message to the argument message.
  116.     # If there is no argument, don't change the current.
  117.     if $?1
  118.     msg_list - $1
  119.     endif
  120.  
  121.     # Eliminate any messages sent by this program.  The cryptic syntax
  122.     # here determines whether an X-Reply-Generated-By header exists.
  123.     if $?[%?x-reply-generated-by?]
  124.     return -1
  125.     endif
  126.  
  127.     # If this message was not explictly sent to the current user,
  128.     # don't send a reply.  This catches most mailing-list mail.
  129.     unset v_to
  130.     pick -r . -t "$user" | set v_to
  131.     if ! $?v_to
  132.     return -1
  133.     endif
  134.     unset v_to
  135.  
  136.     # Compare the sender of the current message to special senders.
  137.     # If there is a match, do not send a reply to this message.
  138.     if $?v_suppress
  139.     set v_suppress = "$v_suppress -request -daemon Postmaster"
  140.     else
  141.     set v_suppress = "-request -daemon Postmaster"
  142.     endif
  143.     foreach sender ($v_suppress) 'v_special from,reply-to $sender'
  144.     if $status == -1
  145.     unset sender
  146.     return -1
  147.     else
  148.     unset sender
  149.     endif
  150.  
  151.     # Examine the Precedence: header for junk-mail values.
  152.     foreach prec (junk bulk) 'v_special precedence $prec'
  153.     if $status == -1
  154.     unset prec
  155.     return -1
  156.     else
  157.     unset prec
  158.     endif
  159.  
  160.     # Remember who sent the message we may be replying to.
  161.     # Strip off the local machine name if present.
  162.     eval -h set from = %a reply_to = %?reply-to?
  163.     if "$from" =~ {,*"$hostname:1"!}"$[%u]"{,@"$hostname:1"*}
  164.     eval -h set from = %u
  165.     # Eliminate any messages sent by the current user.
  166.     if "$from" == "$user"
  167.         unset from
  168.         return -1
  169.     endif
  170.     endif
  171.  
  172.     # If the $v_sent_to folder does not exist or has zero size,
  173.     # no replies have been sent.  Send a reply to this message.
  174.     if ! -e $v_sent_to
  175.     unset from
  176.     return 0
  177.     endif
  178.     if -z $v_sent_to
  179.     unset from
  180.     return 0
  181.     endif
  182.  
  183.     # Remember the name of the current folder.
  184.     set lastfolder = $thisfolder
  185.  
  186.     # Set the variable $sent, to allow testing even if "open" fails.
  187.     set sent
  188.     # The $v_sent_to folder does exist, so open it for checking.
  189.     # If the folder is already open, this will switch to it.
  190.     builtin open -r $v_sent_to | set sent
  191.  
  192.     # If no messages were found, assume a reply should be sent.
  193.     # This is most likely an error condition of some kind.
  194.     if X$sent == X
  195.     unset from sent
  196.     return 0
  197.     endif
  198.     # Clear $sent (it is no longer needed).
  199.     unset sent
  200.  
  201.     # Clear the variable $pastweek.
  202.     unset pastweek
  203.     # Search for replies dated within the past week, sent to
  204.     # the author of the current message.  Save any matches.
  205.     builtin pick -a +1wk | builtin pick -t "$from" | set pastweek
  206.     unset from
  207.  
  208.     # Also check the reply-to address (rudimentary at this time)
  209.     if ! $?pastweek
  210.         if X"$reply_to" != X
  211.             builtin pick -a +1wk | \
  212.                 builtin pick -t "$reply_to" | set pastweek
  213.         endif
  214.     endif
  215.  
  216.     # Return to the previous folder.
  217.     builtin folder -n $lastfolder
  218.     unset lastfolder
  219.  
  220.     # If there were any replies to the current author in the
  221.     # past week, do not reply to the current message.
  222.     if $?pastweek
  223.     unset pastweek
  224.     return -1
  225.     endif
  226.  
  227.     # All tests completed.  A reply should be sent.
  228.     return 0
  229. }
  230.  
  231. function v_special() {
  232. #%
  233. # Check a specified header ($1) against a given pattern ($2).
  234. # If there is a match, return -1 to prevent a reply from being sent.
  235. #%
  236.     if $?v_special
  237.     unset v_special
  238.     endif
  239.     if "$1" == from
  240.     builtin pick -r . -i -f -e $2 | set v_special
  241.     else
  242.     builtin pick -r . -i -h $1 -e $2 | set v_special
  243.     endif
  244.     if $?v_special
  245.     unset v_special
  246.     return -1
  247.     else
  248.     return 0
  249.     endif
  250. }
  251.  
  252. function v_mail() {
  253. #%
  254. # This function is executed to send the vacation message in reply to the
  255. # incoming mail.  It sets the logfile to record the headers of the reply.
  256. # Note that the ~/.zvacation.msg file should NOT contain message headers.
  257. #%
  258.     # Remember the name of the logfile.
  259.     # Other filters may need it later.
  260.     if $?logfile
  261.     set oldlog = $logfile
  262.     endif
  263.     if $?record
  264.     set oldrec = $record
  265.     unset record
  266.     endif
  267.     # Reset the logfile to the $v_sent_to folder.
  268.     set logfile = $v_sent_to
  269.     # If no $v_subject is set, invent one.
  270.     if ! $?v_subject
  271.     set v_subject = "I am on vacation"
  272.     endif
  273.     # Set a special header to indicate automation.
  274.     my_hdr X-Reply-Generated-By: "$version, $v_version" 
  275.     # Generate a reply using the $v_message file.
  276.     # The usual signatures are attached.
  277.     builtin reply $1 -s "$v_subject" -U -H $v_message
  278.     # Remove the special header.
  279.     un_hdr X-Reply-Generated-By:
  280.     # Restore the "new" status of the message,
  281.     # so other filters are not confused.
  282.     flags N $1
  283.     # Restore the previous logfile, if any.
  284.     if $?oldlog
  285.     set logfile = $oldlog
  286.     unset oldlog
  287.     else
  288.     unset logfile
  289.     endif
  290.     if $?oldrec
  291.     set record = $oldrec
  292.     unset oldrec
  293.     endif
  294. }
  295.